﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SisLegal.Model;

namespace SisLegal.Conciliation
{
    public partial class ConciliationDisplayForm : System.Web.UI.Page
    {
        private int ID
        {
            get
            {
                if (Request.QueryString["id"] == null)
                    return 0;
                else
                    return int.Parse(Request.QueryString["id"]);
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                hlUpload.NavigateUrl = "/Conciliation/ConciliationUploadForm.aspx?id=" + ID.ToString() + "&type=" + (int)eFileLoad.Expediente_Principal;
                hlUploadSecond.NavigateUrl = "/Conciliation/ConciliationUploadForm.aspx?id=" + ID.ToString() + "&type=" + (int)eFileLoad.Expediente_Secundary;
                hlEdit.NavigateUrl = "/Conciliation/ConciliationEditForm.aspx?id=" + ID.ToString();
                
                DDL_Participation_Seal.DataSource = Enumeration.GetAll<eParticipation_Seal_Fiscal_Folder>();
                DDL_Participation_Seal.DataTextField = "Value";
                DDL_Participation_Seal.DataValueField = "Key";
                DDL_Participation_Seal.DataBind();

                FileLoadManager file = new FileLoadManager();
                FilePrincipal.DataSource = file.GetAllFile(ID, (int)eFileLoad.Expediente_Principal);
                FilePrincipal.DataBind();

                FileSecundary.DataSource = file.GetAllFile(ID, (int)eFileLoad.Expediente_Secundary);
                FileSecundary.DataBind();
                //---------------------------------------------------------
                Model.ConciliationManager manager = new Model.ConciliationManager();
                Model.Conciliation obj = manager.GetById(ID);

                this.txtNroConciliation.Text = obj.NroConciliation;
                this.txtCity.Text = obj.City;
                this.txtSolicitante.Text = obj.Solicitante;
                this.txtInvitado.Text = obj.Invitado;
                this.txtNroConciliation.Text = obj.NroConciliation;

                DDL_Participation_Seal.ClearSelection();
                DDL_Participation_Seal.Items.FindByValue(obj.ParticipationSeal.ToString()).Selected = true;

                this.txtDateInvitation.Text = String.Format("{0:dd/MM/yyyy}", obj.DateInvitation);
                this.txtFirstCitation.Text = String.Format("{0:dd/MM/yyyy}", obj.FirstCitation);
                this.txtSecondCitation.Text = String.Format("{0:dd/MM/yyyy}", obj.SecondCitation);

                this.txtSumilla.Text = obj.Sumilla;
                this.txtObservaciones.Text = obj.Observaciones;
            }
        }

        protected void Delete_Click(object sender, EventArgs e)
        {
            Model.ConciliationManager manager = new Model.ConciliationManager();
            manager.Delete(ID);
            Response.Redirect("/Conciliation/ConciliationListView.aspx");

        }        

        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            Int32 idFile = Convert.ToInt32(e.CommandArgument);
            if (e.CommandName == "download")
            {
                Download(idFile);
            }
            if (e.CommandName == "delete")
            {
                Model.FileLoadManager manager = new Model.FileLoadManager();
                manager.Delete(idFile);

                FileLoadManager file = new FileLoadManager();
                FilePrincipal.DataSource = file.GetAllFile(ID, (int)eFileLoad.Expediente_Principal);
                FilePrincipal.DataBind();

                FileSecundary.DataSource = file.GetAllFile(ID, (int)eFileLoad.Expediente_Secundary);
                FileSecundary.DataBind();
            }
        }

        public void Download(int ID)
        {
            Model.FileLoadManager manager = new Model.FileLoadManager();
            FileLoad file = manager.GetById(ID);

            string content_type = "";

            if (String.ReferenceEquals(file.Ext, ".htm") || String.ReferenceEquals(file.Ext, ".html"))
            {
                content_type = "text/HTML";
            }
            if (String.ReferenceEquals(file.Ext, ".txt"))
            {
                content_type = "text/plain";
            }
            if (String.ReferenceEquals(file.Ext, ".doc") || String.ReferenceEquals(file.Ext, ".rtf") || String.ReferenceEquals(file.Ext, ".docx"))
            {
                content_type = "Application/msword";
            }
            if (String.ReferenceEquals(file.Ext, ".xls") || String.ReferenceEquals(file.Ext, ".xlsx"))
            {
                content_type = "Application/x-msexcel";
            }
            if (String.ReferenceEquals(file.Ext, ".jpg") || String.ReferenceEquals(file.Ext, ".jpeg"))
            {
                content_type = "image/jpeg";
            }
            if (String.ReferenceEquals(file.Ext, ".gif"))
            {
                content_type = "image/GIF";
            }
            if (String.ReferenceEquals(file.Ext, ".pdf"))
            {
                content_type = "Application/pdf";
            }

            Response.ContentType = content_type;
            Response.AppendHeader("Content-Disposition", "attachment; filename=" + file.Name);
            Response.TransmitFile(Server.MapPath("~/Uploads/" + file.Guid));
            Response.End();
        }
    }
}